import sys
import logging
import modbus_tk
import modbus_tk.defines as cst
import modbus_tk.modbus_tcp as modbus_tcp
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QTimer

logger = modbus_tk.utils.create_logger("console")
if __name__ == "__main__":
    try:
        # 连接MODBUS TCP从机
        master = modbus_tcp.TcpMaster("192.168.1.5",502)
        master.set_timeout(5.0)
        logger.info("connected")
        # 读保持寄存器
        #i=logger.info(master.execute(1, cst.READ_HOLDING_REGISTERS, 0,2))
        j = master.execute(1, cst.READ_HOLDING_REGISTERS, 0,2)
        print("j:",j)
    except ValueError:
        print("Error!")

class Ui_MainWindow(object):  # 继承父类QObject
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)  # 创建一个中央元件centralwidget，并把它加入到窗口MainWindow
        self.centralwidget.setObjectName("centralwidget")  # 命名
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)  # 命令按钮
        self.pushButton.setGeometry(QtCore.QRect(200, 60, 361, 71))
        self.pushButton.setObjectName("pushButton")
        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(190, 200, 401, 261))
        self.textEdit.setObjectName("textEdit")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.timer = QTimer(self)  # 初始化一个定时器
        self.timer.timeout.connect(self.operate)  # 计时结束调用operate()方法
        self.pushButton.clicked.connect(self.buttonOnClick)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)  # 关联信号槽

    def buttonOnClick(self):
        self.timer.start(1000)  # 设置计时间隔并启动

    def operate(self):
        j = master.execute(1, cst.READ_HOLDING_REGISTERS, 0, 2)
        print("j:", j)
        self.textEdit.setText("Timer start !!!\n")

        if j[1]==0:
            self.textEdit.append("j[1] = 0 \n")
            self.case0()
        elif j[1]==1:
            self.textEdit.append("j[1] = 1 \n")
            self.case1()
        elif j[1]==2:
            self.textEdit.append("j[1] = 2 \n")
            self.case2()
        else:
            self.textEdit.append("j[1] = 3 \n")
            self.case3()


    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "按钮"))

    # 定义槽函数
    def case0(self):
        self.textEdit.setText("1,2号窗户均关闭")

    def case1(self):
        self.textEdit.setText("1号窗户打开,2号窗户关闭")

    def case2(self):
        self.textEdit.setText("1号窗户关闭,2号窗户打开")

    def case3(self):
        self.textEdit.setText("1,2号窗户均被打开")

class mywindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(mywindow, self).__init__()
        self.setupUi(self)


if j[1] == 65504:
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No door open !!!" + "\n")
elif j[1] == 65505:
    sock_client_alarmDoor()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 1 door open !!!" + "\n")
elif j[1] == 65506:
    sock_client_alarmDoor1()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 2 door open !!!" + "\n")
elif j[1] == 65507:
    sock_client_alarmDoor()
    sock_client_alarmDoor1()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 1&2 door open !!!" + "\n")
elif j[1] == 65508:
    sock_client_alarmDoor2()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 3 door open !!!" + "\n")
elif j[1] == 65509:
    sock_client_alarmDoor()
    sock_client_alarmDoor2()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 1&3 door open !!!" + "\n")
elif j[1] == 65510:
    sock_client_alarmDoor1()
    sock_client_alarmDoor2()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 2&3 door open !!!" + "\n")
elif j[1] == 65511:
    sock_client_alarmDoor()
    sock_client_alarmDoor1()
    sock_client_alarmDoor2()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 1&2&3 door open !!!" + "\n")
elif j[1] == 65512:
    sock_client_alarmDoor3()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 4 door open !!!" + "\n")
elif j[1] == 65513:
    sock_client_alarmDoor()
    sock_client_alarmDoor3()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 1&4 door open !!!" + "\n")
elif j[1] == 65514:
    sock_client_alarmDoor1()
    sock_client_alarmDoor3()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 2&4 door open !!!" + "\n")
elif j[1] == 65515:
    sock_client_alarmDoor()
    sock_client_alarmDoor1()
    sock_client_alarmDoor3()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 1&2&4 door open !!!" + "\n")
elif j[1] == 65516:
    sock_client_alarmDoor2()
    sock_client_alarmDoor3()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 3&4 door open !!!" + "\n")
elif j[1] == 65517:
    sock_client_alarmDoor()
    sock_client_alarmDoor2()
    sock_client_alarmDoor3()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 1&3&4 door open !!!" + "\n")
elif j[1] == 65518:
    sock_client_alarmDoor1()
    sock_client_alarmDoor2()
    sock_client_alarmDoor3()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 2&3&4 door open !!!" + "\n")
elif j[1] == 65519:
    sock_client_alarmDoor()
    sock_client_alarmDoor1()
    sock_client_alarmDoor2()
    sock_client_alarmDoor3()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 1&2&3&4 door open !!!" + "\n")
elif j[1] == 65520:
    sock_client_alarmDoor4()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 5 door open !!!" + "\n")
elif j[1] == 65521:
    sock_client_alarmDoor()
    sock_client_alarmDoor4()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 1&5 door open !!!" + "\n")
elif j[1] == 65522:
    sock_client_alarmDoor1()
    sock_client_alarmDoor4()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 2&5 door open !!!" + "\n")
elif j[1] == 65523:
    sock_client_alarmDoor()
    sock_client_alarmDoor1()
    sock_client_alarmDoor4()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 1&5 door open !!!" + "\n")
elif j[1] == 65524:
    sock_client_alarmDoor2()
    sock_client_alarmDoor4()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 3&5 door open !!!" + "\n")
elif j[1] == 65525:
    sock_client_alarmDoor()
    sock_client_alarmDoor2()
    sock_client_alarmDoor4()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 1&3&5 door open !!!" + "\n")
elif j[1] == 65526:
    sock_client_alarmDoor1()
    sock_client_alarmDoor2()
    sock_client_alarmDoor4()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 2&3&5 door open !!!" + "\n")
elif j[1] == 65527:
    sock_client_alarmDoor()
    sock_client_alarmDoor1()
    sock_client_alarmDoor2()
    sock_client_alarmDoor4()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 1&2&3&5 door open !!!" + "\n")
elif j[1] == 65528:
    sock_client_alarmDoor3()
    sock_client_alarmDoor4()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 4&5 door open !!!" + "\n")
elif j[1] == 65529:
    sock_client_alarmDoor()
    sock_client_alarmDoor3()
    sock_client_alarmDoor4()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 1&4&5 door open !!!" + "\n")
elif j[1] == 65530:
    sock_client_alarmDoor1()
    sock_client_alarmDoor3()
    sock_client_alarmDoor4()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 2&4&5 door open !!!" + "\n")
elif j[1] == 65531:
    sock_client_alarmDoor()
    sock_client_alarmDoor1()
    sock_client_alarmDoor3()
    sock_client_alarmDoor4()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 1&2&4&5 door open !!!" + "\n")
elif j[1] == 65532:
    sock_client_alarmDoor2()
    sock_client_alarmDoor3()
    sock_client_alarmDoor4()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 3&4&5 door open !!!" + "\n")
elif j[1] == 65533:
    sock_client_alarmDoor()
    sock_client_alarmDoor2()
    sock_client_alarmDoor3()
    sock_client_alarmDoor4()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 1&3&4&5 door open !!!" + "\n")
elif j[1] == 65534:
    sock_client_alarmDoor1()
    sock_client_alarmDoor2()
    sock_client_alarmDoor3()
    sock_client_alarmDoor4()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 2&3&4&5 door open !!!" + "\n")
elif j[1] == 65535:
    sock_client_alarmDoor()
    sock_client_alarmDoor1()
    sock_client_alarmDoor2()
    sock_client_alarmDoor3()
    sock_client_alarmDoor4()
    self.textEdit1.append("*** 时间： " + timeDisplay + "\n"
                          + "No 12&&3&4&5 door open !!!" + "\n")

'''
            if j[1] == 65504:
                self.textEdit1.append("*** 时间： " + timeDisplay  +"\n"
                              +"No door open !!!" +"\n")

            elif j[1] == 65505 or 65507 or 65509 or 65513 or 65521 or 65511 or 65515 or 65523 or 65517 or 65525 or 65529 \
                    or 65519 or 65527 or 65533 or 65531 or 65535:
                sock_client_alarmDoor()
                self.textEdit1.append("*** 时间： " + timeDisplay  +"\n"
                              +"No 1 door open !!!" +"\n")

            elif j[1] == 65506 or 65507 or 65510 or 65514 or 65522 or 65511 or 65515 or 65523 or 65518 or 65526 or 65530\
                    or 65519 or 65527 or 65534 or 65531 or 65535:
                sock_client_alarmDoor1()
                self.textEdit1.append("*** 时间： " + timeDisplay  +"\n"
                              +"No 2 door open !!!" +"\n")

            elif j[1] == 65508 or 65509 or 65510 or 65516 or 65524 or 65511 or 65517 or 65525 or 65518 or 65526 or 65532\
                    or 65519 or 65527 or 65534 or 65533 or 65535:
                sock_client_alarmDoor2()
                self.textEdit1.append("*** 时间： " + timeDisplay  +"\n"
                              +"No 3 door open !!!" +"\n")

            elif j[1] == 65512 or 65513 or 65514 or 65516 or 65528 or 65515 or 65517 or 65529 or 65518 or 65530 or 65532 \
                    or 65519 or 65534 or 65533 or 65531 or 65535:
                sock_client_alarmDoor3()
                self.textEdit1.append("*** 时间： " + timeDisplay  +"\n"
                              +"No 4 door open !!!" +"\n")

            elif j[1] == 65520 or 65521 or 65522 or 65524 or 65528 or 65523 or 65525 or 65529 or 65526 or 65530 or 65532 \
                    or 65527 or 65534 or 65533 or 65531 or 65535 :
                sock_client_alarmDoor4()
                self.textEdit1.append("*** 时间： " + timeDisplay  +"\n"
                              +"No 5 door open !!!" +"\n")
'''